﻿module main

open Ast
open System.IO
open Printf
open Microsoft.FSharp.Text.Lexing
open Microsoft.FSharp.Text.Parsing


let WriteFile(fn : string, txt : string) =
    let strm = new StreamWriter(fn) 
    strm.Write(txt)
    strm.Close()

let _ =
      if Array.length fsi.CommandLineArgs <> 2 || not (File.Exists(fsi.CommandLineArgs.[1]))
      then begin
            printf "usage: pic <file>\n";
            exit 1
      end;

      let filename = fsi.CommandLineArgs.[1] in
      let proc = ref Inert in

      using (new BinaryReader(new FileStream(filename, System.IO.FileMode.Open))) (fun input ->
            proc :=
                  let lexbuf = Microsoft.FSharp.Text.Lexing.LexBuffer<byte>.FromBinaryReader input in
                  try Pars.start Lex.token lexbuf
                  with e ->
                        let pos = Lexing.Position.Empty in
                        printf "%s (%d, %d): parse error: %s"
                              filename
                              (pos.pos_lnum+1)
                              (pos.pos_cnum - pos.pos_bol + 1)
                              (e.Message);
                        exit 1;
                        Inert
      );
      printf "%A" !proc

      let text = sprintf "%A" !proc
      WriteFile("outputfile.txt", text)




// end-of-file